<?php

namespace app\modules\sample\models;

use Yii;

/**
 * This is the model class for table "sample_position_else".
 *
 * @property integer $pk_id
 * @property string $from_id
 * @property string $xna_id
 * @property integer $xna_fridge
 * @property string $case
 * @property integer $case_a
 * @property integer $case_b
 * @property integer $xna_x
 * @property integer $xna_y
 * @property integer $xna_z
 * @property integer $xna_density
 * @property integer $xna_volumn
 * @property integer $xna_volumn_left
 * @property string $crt_name
 * @property string $crt_time
 */
class SamplePositionElse extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'sample_position_else';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['from_id', 'xna_id', 'xna_volumn', 'crt_name', 'crt_time'], 'required'],
            [['xna_fridge', 'xna_x', 'xna_y', 'xna_z'], 'integer'],
            [['xna_density', 'xna_volumn', 'xna_volumn_left'], 'number'],
            [['case_a', 'case_b'], 'string', 'max' => 2],
            [['crt_time'], 'safe'],
            [['from_id', 'xna_id', 'case'], 'string', 'max' => 20],
            [['crt_name'], 'string', 'max' => 10],
            [['xna_id'], 'unique']
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'pk_id' => 'Pk ID',
            'xna_id' => '编号',
            'xna_fridge' => '所在冰箱',
            'case' => '盒名',
            'case_a' => 'A',
            'case_b' => 'B',
            'xna_x' => 'X',
            'xna_y' => 'Y',
            'xna_z' => 'Z',
            'xna_density' => '浓度(ng/uL)',
            'xna_volumn' => '初始体积(uL)',
            'xna_volumn_left' => '总体积(uL)',
            'crt_name' => '修改人',
            'crt_time' => '修改时间',
        ];
    }

    public function getAlter()
    {
        return $this->hasMany(SampleAlter::className(), ['from_id' => 'pk_id']);
    }

    public function isEnough($volumnUsed)
    {
        $this->xna_volumn_left = $this->xna_volumn_left - intval($volumnUsed);
        if ($this->xna_volumn_left < 0) {
            return $this->xna_id.'剩余体积不足';
        } else if ($this->xna_volumn_left == 0) {
            $this->xna_fridge = 0;
        }

        return 'success';
    }

    public function isNewRecord()
    {
        if ($this->pk_id != '') {
            return false;
        }

        return true;
    }
}
